package edu.thu.studio.ormdesigner.editor.policy;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.requests.GroupRequest;

import edu.thu.studio.ormdesigner.editor.command.DeleteLinkCommand;
import edu.thu.studio.ormdesigner.editor.command.DeleteTableNodeCommand;
import edu.thu.studio.ormdesigner.editor.command.DeleteViewNodeCommand;
import edu.thu.studio.ormdesigner.editor.editpart.DeleteableEditPart;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.link.Link;
import edu.thu.studio.ormdesigner.model.diagram.node.Node;
import edu.thu.studio.ormdesigner.model.diagram.node.NodeSet;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNode;
import edu.thu.studio.ormdesigner.model.diagram.node.ViewNode;
import edu.thu.studio.ormdesigner.util.Utils;

public class NodeComponentEditPolicy extends ComponentEditPolicy {
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected Command createDeleteCommand(GroupRequest request) {
        try {
            if (this.getHost() instanceof DeleteableEditPart) {
                DeleteableEditPart editPart = (DeleteableEditPart) this.getHost();
                
                if (editPart.isDeleteable()) {
                    
                    Set<Node> nodes = new HashSet<Node>();
                    Set<Link> links = new HashSet<Link>();
                    
                    for (Object object : request.getEditParts()) {
                        EditPart _editPart = (EditPart) object;
                        
                        Object model = _editPart.getModel();
                        
                        if (model instanceof Node) {
                            Node node = (Node) model;
                            nodes.add(node);
                            // 如果是删除非快照，则需要级连删除所有的快照
                            if (node.isOriginal()) {
                                
                                // 迭代所有的package 查看引用
                                for (Diagram diagram : Utils.getORMModel().getDiagrams()) {
                                    NodeSet nodeSet = diagram.getNodes();
                                    nodes.addAll(nodeSet.filterByRefId(node.getRefId()));
                                }
                            }
                        }
                        
                    }
                    CompoundCommand command = new CompoundCommand();
                    
                    for (Node node : nodes) {
                        for (Link link : node.getIncomings()) {
                            if (!links.contains(link)) {
                                links.add(link);
                                command.add(new DeleteLinkCommand(link));
                            }
                        }
                        for (Link link : node.getOutgoings()) {
                            if (!links.contains(link)) {
                                links.add(link);
                                command.add(new DeleteLinkCommand(link));
                            }
                        }
                        if (node instanceof TableNode) {
                            command.add(new DeleteTableNodeCommand(Utils.getDiagram(node), (TableNode) node));
                        } else {
                            command.add(new DeleteViewNodeCommand(Utils.getDiagram(node), (ViewNode) node));
                        }
                    }
                    return command.unwrap();
                }
            }
            
        } catch (Exception e) {
            Utils.showExceptionDialog(e);
        }
        
        return null;
    }
}
